
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.PubgConstant;
import com.jf.cloud.service.sysmanager.constant.TradeType;
import com.jf.cloud.service.sysmanager.constant.UserConstant;
import com.jf.cloud.service.sysmanager.constant.UserMatchConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgFinanceFlowDetailService;
import com.jf.cloud.service.sysmanager.dao.IPubgTeamService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserTeamService;
import com.jf.cloud.service.sysmanager.entity.PubgFinanceFlowDetail;
import com.jf.cloud.service.sysmanager.entity.PubgTeam;
import com.jf.cloud.service.sysmanager.entity.PubgUserMatch;
import com.jf.cloud.service.sysmanager.entity.PubgUserTeam;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgTeamService")
public class PubgTeamServiceImpl extends BaseService<PubgTeam, Long> implements IPubgTeamService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgTeamServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;
	
	@Resource
	private IPubgUserTeamService pubgUserTeamService;
	
	@Resource
	private IPubgFinanceFlowDetailService pubgFinanceFlowDetailService;

	/**
	 * 根据角色获取用户的战队
	 * 
	 * @author 朱建文 创建时间：2018年3月31日 下午5:53:33
	 */
	public List<Map<String, Object>> findPubgTeamListByRole(OPaging<Map<String, Object>> page) {
		Paging<Map<String, Object>> dataPage = new Paging<Map<String, Object>>();
		try {
			StringBuffer sb = new StringBuffer(255);
			String userRole = page.getWm().get("non_USER_ROLE") + "";
			if (!StringUtil.isInteger(userRole)) {
				return dataPage.getDataList();
			}
			  
			// 队员 查看【我的战队】
			if (UserConstant.USER_ROLE_10.getId().equals(Integer.valueOf(userRole))) {
				page.getWm().put("RELATION_STATE", PubgConstant.USER_TEAM_RELATION_STATE_2);
				sb.append(" SELECT ");
				sb.append("   b.ROW_ID rowId, IFNULL(b.TEAM_NO, '') teamNo, b.TEAM_NAME teamName, b.MEMBER_NUM memberNum, b.TEAM_PHOTO teamPhoto, RELATION_STATE relationState, ");
				sb.append("   b.PLACE_PROVINCE placeProvince, b.PLACE_CITY placeCity, b.PLACE_DISTRICT placeDistrict, TEAM_ADDRESS teamAddress, ");
				sb.append("   b.PLACE_LONGITUDE placeLongitude, b.PLACE_LATITUDE placeLatitude, b.NOT_END_MATCH_COUNT notEndMatchCount ");
				sb.append(" FROM ");
				sb.append("   pubg_user_team a LEFT JOIN pubg_team b ON a.TEAM_ID = b.ROW_ID ");
				sb.append(page.getWhereSQL(true));
				dataPage = this.baseDao.findObjectListMapByPage(sb.toString(), page, page.getParamList().toArray());
				
			} else if (UserConstant.USER_ROLE_20.getId().equals(Integer.valueOf(userRole))) {
				sb = new StringBuffer(255);
				sb.append(" SELECT ");
				sb.append("   b.ROW_ID rowId, b.TEAM_NO teamNo, b.TEAM_NAME teamName, b.TEAM_INTRO teamIntro, ");
				sb.append("   b.PLACE_PROVINCE placeProvince, b.PLACE_CITY placeCity, b.PLACE_DISTRICT placeDistrict, ");
				sb.append("   b.PLACE_LONGITUDE placeLongitude, b.PLACE_LATITUDE placeLatitude, TEAM_ADDRESS teamAddress, ");
				sb.append("   b.INVITE_CODE inviteCode, b.CAPTAIN_ACCOUNT captainAccount ");
				sb.append(" FROM ");
				sb.append("   pubg_team b ");
				sb.append(" WHERE ");
				sb.append("   b.CAPTAIN_ID LIKE ('%" + page.getWm().get("USER_ID") + ",%')");
				dataPage = this.baseDao.findObjectListMapByPage(sb.toString(), page);
				 
			} else if (UserConstant.USER_ROLE_30.getId().equals(Integer.valueOf(userRole))) {
				sb = new StringBuffer(255);
				sb.append(" SELECT ");
				sb.append("   b.TEAM_NAME teamName, IFNULL(b.TEAM_INTRO, '') teamIntro, ");
				sb.append("   b.ROW_ID rowId, IFNULL(MEMBER_NUM, 0) memberNum, IFNULL(NOT_END_MATCH_COUNT, 0) notEndMatchCount, ");
				sb.append("   IFNULL(b.PLACE_PROVINCE, '') placeProvince, IFNULL(b.PLACE_CITY, '') placeCity, IFNULL(b.PLACE_DISTRICT, '') placeDistrict, ");
				sb.append("   IFNULL(b.PLACE_LONGITUDE, '') placeLongitude, IFNULL(b.PLACE_LATITUDE, '') placeLatitude, IFNULL(TEAM_ADDRESS, '') teamAddress ");
				sb.append(" FROM ");
				sb.append("   pubg_club a LEFT JOIN pubg_team b ON a.ROW_ID = b.CLUB_ID ");
				sb.append(" WHERE ");
				sb.append("   a.MINISTER_ID LIKE ('%" + page.getWm().get("USER_ID") + ",%')");
				dataPage = this.baseDao.findObjectListMapByPage(sb.toString(), page);

			}
		} catch (Exception e) {
			log.error("根据角色获取用户的战队出错！", e);
		}
		return dataPage.getDataList();
	}

	/**
	 * 获取战队详情
	 * 
	 * @author 朱建文 创建时间：2018年3月31日 下午5:53:33
	 */
	public Map<String, Object> findPubgTeamDetail(Long userId, String userRole, Long rowId) throws Exception {
		Map<String, Object> dataMap = null;
		StringBuffer sb = new StringBuffer(255);
		
		// 队员 查看【我的战队】
		if (UserConstant.USER_ROLE_10.getId().equals(Integer.valueOf(userRole))) {
			sb = new StringBuffer(255);
			sb.append(" SELECT ");
			sb.append("   b.ROW_ID rowId, b.TEAM_NO teamNo, b.TEAM_NAME teamName, b.MEMBER_NUM memberNum, b.PLACE_PROVINCE placeProvince, CAPTAIN_ACCOUNT captainAccount, ");
			sb.append("   b.PLACE_CITY placeCity, b.PLACE_DISTRICT placeDistrict, b.PLACE_LONGITUDE placeLongitude, TEAM_INTRO teamIntro,");
			sb.append("   b.PLACE_LATITUDE placeLatitude, b.TEAM_PHOTO teamPhoto, b.NOT_END_MATCH_COUNT notEndMatchCount, CREATE_TIME createTime ");
			sb.append(" FROM ");
			sb.append("   pubg_team b ");
			sb.append(" WHERE ");
			sb.append("   b.ROW_ID = ? ");
			dataMap = this.baseDao.findMap(sb.toString(), rowId);
			
		} else if (UserConstant.USER_ROLE_20.getId().equals(Integer.valueOf(userRole))) {
			sb = new StringBuffer(255);
			sb.append(" SELECT ");
			sb.append("   b.ROW_ID rowId, b.TEAM_NO teamNo, b.TEAM_NAME teamName, b.PLACE_PROVINCE placeProvince, ");
			sb.append("   b.PLACE_CITY placeCity, b.PLACE_DISTRICT placeDistrict, b.PLACE_LONGITUDE placeLatitude, ");
			sb.append("   b.PLACE_LATITUDE placeLatitude, b.INVITE_CODE inviteCode, b.CAPTAIN_ACCOUNT captainAccount, b.TEAM_INTRO teamIntro, TEAM_PHOTO teamPhoto ");
			sb.append(" FROM ");
			sb.append("   pubg_team b ");
			sb.append(" WHERE ");
			sb.append("   b.CAPTAIN_ID LIKE ('%" + userId +",%')");
			dataMap = this.baseDao.findMap(sb.toString());
			
		} else if (UserConstant.USER_ROLE_30.getId().equals(Integer.valueOf(userRole))) {
			sb = new StringBuffer(255);
			sb.append(" SELECT ");
			sb.append("   b.TEAM_NAME teamName, IFNULL(b.TEAM_INTRO, '') teamIntro, ");
			sb.append("   b.ROW_ID rowId, IFNULL(b.TEAM_BALANCE, 0) teamBalance, IFNULL(NOT_END_MATCH_COUNT, 0) notEndMatchCount, ");
			sb.append("   IFNULL(b.PLACE_PROVINCE, '') placeProvince, IFNULL(b.PLACE_CITY, '') placeCity, IFNULL(b.PLACE_DISTRICT, '') placeDistrict, ");
			sb.append("   IFNULL(b.PLACE_LONGITUDE, '') placeLongitude, IFNULL(b.PLACE_LATITUDE, '') placeLatitude, IFNULL(TEAM_ADDRESS, '') teamAddress, ");
			sb.append("   IFNULL(b.TEAM_NO, '') teamNo, IFNULL(SETTLEMENT_RATIO, 0) settlementRatio, IFNULL(SETTLEMENT_WAY, '') settementWay, ");
			sb.append("   IFNULL(b.INVITE_CODE, '') inviteCode, IFNULL(b.CAPTAIN_ACCOUNT, '') captainAccount, IFNULL(TEAM_PHOTO, '') teamPhoto ");
			sb.append(" FROM ");
			sb.append("   pubg_club a LEFT JOIN pubg_team b ON a.ROW_ID = b.CLUB_ID ");
			sb.append(" WHERE ");
			sb.append("   b.ROW_ID = ? AND a.MINISTER_ID LIKE ('%" + userId +",%')");
			dataMap = this.baseDao.findMap(sb.toString(), rowId);
			
		}
		return dataMap;
	}

	/**
	 * 添加/更新战队
	 * 
	 * @author 朱建文 创建时间：2018年4月3日 下午3:05:15
	 */
	@Transactional
	public boolean addPubgTeam(PubgTeam pubgTeam) throws Exception {
		boolean boo = false;
		try {
			if (pubgTeam == null) {
				return boo;
			}
			
			// 队长的ID
			String captainIds = pubgTeam.getCaptainId();
			if (captainIds.lastIndexOf(",") == captainIds.length() - 1) {
				captainIds = captainIds.substring(0, captainIds.length() - 1);
			}
			String sql = "UPDATE pubg_user SET USER_ROLE = ? ";
			sql += " WHERE ROW_ID IN (" + captainIds + ") AND USER_ROLE = ? ";
			
			// rowId 不为空时 表示更新
			if (pubgTeam.getRowId() != null) {
				
				// 更新时 需要注意队长帐号的变化与否
				if ("1".equals(pubgTeam.getCaptainChangeFlag())) {
					String selectSql = "SELECT CAPTAIN_ID FROM pubg_team WHERE ROW_ID = ?";
					String captainIdsOld = this.baseDao.find(selectSql, String.class, pubgTeam.getRowId());
					if (captainIdsOld.lastIndexOf(",") == captainIdsOld.length() - 1) {
						captainIdsOld = captainIdsOld.substring(0, captainIdsOld.length() - 1);
					}
					
					// 更新老队长的角色
					this.baseDao.safeUpdate(sql, captainIds.split(",").length, 
							UserConstant.USER_ROLE_10.getId(), UserConstant.USER_ROLE_20.getId());
					
					// 更新新队长的角色
					this.baseDao.safeUpdate(sql, captainIds.split(",").length, 
							UserConstant.USER_ROLE_20.getId(), UserConstant.USER_ROLE_10.getId());
				}
				
				this.saveObj(pubgTeam);
				return true;
			}
			
			// 新增时 更新队长角色
			this.baseDao.safeUpdate(sql, captainIds.split(",").length, 
					UserConstant.USER_ROLE_20.getId(), UserConstant.USER_ROLE_10.getId());
			
			pubgTeam.setMemberNum(0);
			pubgTeam.setNotEndMatchCount(0);
			pubgTeam.setRechargeSum(0.0);
			pubgTeam.setTeamBalance(0.0);
			pubgTeam.setSettlementWay("10");
			pubgTeam.setCreateTime(DateUtil.getCurrentDateTime());
			pubgTeam = this.saveObj(pubgTeam);
		} catch (Exception e) {
			throw new RuntimeException("添加/更新战队出错！", e);
		}
		return boo;
	}
	
	/**
	 * 队长审核队员的入队申请
	 * 
	 * @author 朱建文 创建时间：2018年4月4日 下午1:20:19
	 */
	@Transactional
	public void updateMemberRelationState(PubgUserTeam pubgUserTeam, Integer relationState) throws Exception {
		try {
			
			// 更新队员-战队关系
			StringBuffer sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_user_team ");
			sb.append(" SET ");
			sb.append("   RELATION_STATE = ?, AUDIT_ID = ?, AUDIT_ACCOUNT = ?, AUDIT_TIME = ? ");
			sb.append(" WHERE ");
			sb.append("   USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND RELATION_STATE = ? ");
			this.baseDao.safeUpdate(sb.toString(), 1, relationState, 
					pubgUserTeam.getAuditId(), pubgUserTeam.getAuditAccount(), DateUtil.getCurrentDateTime(),
					pubgUserTeam.getUserId(), pubgUserTeam.getTeamId(), pubgUserTeam.getClubId(), PubgConstant.USER_TEAM_RELATION_STATE_1);
			
			// 更新队员-俱乐部关系
			sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_user_club ");
			sb.append(" SET ");
			sb.append("   RELATION_STATE = ?, AUDIT_TIME = ? ");
			sb.append(" WHERE ");
			sb.append("   USER_ID = ? AND CLUB_ID = ? AND RELATION_STATE = ? ");
			this.baseDao.safeUpdate(sb.toString(), 1, relationState, DateUtil.getCurrentDateTime(), 
					pubgUserTeam.getUserId(), pubgUserTeam.getClubId(), PubgConstant.USER_TEAM_RELATION_STATE_1);
			
			// 查询当前战队的总队员数
			sb = new StringBuffer();
			sb.append(" SELECT COUNT(0) ");
			sb.append(" FROM pubg_user_team ");
			sb.append(" WHERE USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND RELATION_STATE = ?");
			int i = this.baseDao.findCount(sb.toString(), 
					pubgUserTeam.getUserId(), pubgUserTeam.getTeamId(), pubgUserTeam.getClubId(), 
					PubgConstant.USER_TEAM_RELATION_STATE_2);
			
			// 更新当前战队的总队员数
			sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_team ");
			sb.append(" SET ");
			sb.append("   MEMBER_NUM = ? ");
			sb.append(" WHERE ");
			sb.append("   ROW_ID = ? AND CLUB_ID = ?");
			this.baseDao.update(sb.toString(), i, pubgUserTeam.getTeamId(), pubgUserTeam.getClubId());
		} catch (Exception e) {
			throw new RuntimeException("队长审核队员的入队申请出错！", e);
		}
	}

	/**
	 * 更新队员的余额 并插入流水
	 * 
	 * @author 朱建文 创建时间：2018年4月4日 下午5:21:28
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) //任何异常 都需要回滚
    public void updateTeamMemberBalance(PubgUserTeam pubgUserTeam, Integer optType, Double optMoney) throws Exception {
    	
		// 队员所在战队的余额
		Double userTeamBalance = pubgUserTeam.getUserTeamBalance();
		userTeamBalance = userTeamBalance == null ? 0 : userTeamBalance;
		
		// 队员所在俱乐部的余额
		Double userClubBalance = pubgUserTeam.getRechargeSum();
		userClubBalance = userClubBalance == null ? 0 : userClubBalance;
		
		// 更新队员在改战队下的余额
		StringBuffer sb = new StringBuffer();
		sb.append(" UPDATE ");
		sb.append("   pubg_user_team ");
		sb.append(" SET ");
		sb.append("   USER_TEAM_BALANCE = USER_TEAM_BALANCE + ? ");
		
		// 如果是加款需要更新 充值总额？？？？？
		if (optType == 1) {
			sb.append(" , RECHARGE_SUM = RECHARGE_SUM + " + optMoney + " ");
		}
		sb.append(" WHERE ");
    	sb.append("   USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND RELATION_STATE = 2 AND USER_TEAM_BALANCE = ? ");
		this.baseDao.safeUpdate(sb.toString(), 1, optMoney, pubgUserTeam.getUserId(), 
				pubgUserTeam.getTeamId(), pubgUserTeam.getClubId(), userTeamBalance);
    	
		// 更新队员在改俱乐部下的余额
		sb = new StringBuffer();
		sb.append(" UPDATE ");
		sb.append("   pubg_user_club ");
		sb.append(" SET ");
		sb.append("   USER_CLUB_BALANCE = USER_CLUB_BALANCE + ? ");
		
		// 如果是加款需要更新 充值总额？？？？？
		if (optType == 1) {
			sb.append(" , RECHARGE_SUM = RECHARGE_SUM + " + optMoney + " ");
		}
		sb.append(" WHERE ");
    	sb.append("   USER_ID = ? AND CLUB_ID = ? AND RELATION_STATE = 2 AND USER_CLUB_BALANCE = ? ");
		this.baseDao.safeUpdate(sb.toString(), 1, optMoney, 
				pubgUserTeam.getUserId(), pubgUserTeam.getClubId(), userClubBalance);
		
		// 更新队员总金额
		sb = new StringBuffer();
    	sb.append(" UPDATE ");
    	sb.append("   pubg_user ");
    	sb.append(" SET ");
    	sb.append("   BALANCE = BALANCE + ? ");
    	sb.append(" WHERE ");
    	sb.append("   ROW_ID = ? AND BALANCE = ? ");
    	this.safeUpdate(sb.toString(), 1, optMoney, pubgUserTeam.getUserId(), pubgUserTeam.getWinSum());
    	
		// 插入资金流水
    	PubgFinanceFlowDetail pubgFinanceFlowDetail = new PubgFinanceFlowDetail();
    	pubgFinanceFlowDetail.setClubId(pubgUserTeam.getClubId());
    	pubgFinanceFlowDetail.setClubName(pubgUserTeam.getClubName());
    	pubgFinanceFlowDetail.setInOutFlag(optType);
    	pubgFinanceFlowDetail.setCreateTime(DateUtil.getCurrentDateTime());
    	pubgFinanceFlowDetail.setTeamId(pubgUserTeam.getTeamId());
    	pubgFinanceFlowDetail.setTeamName(pubgUserTeam.getTeamName());
    	pubgFinanceFlowDetail.setUserId(pubgUserTeam.getUserId());
    	pubgFinanceFlowDetail.setUserAccount(pubgUserTeam.getUserAccount());
    	pubgFinanceFlowDetail.setTradeMoney(optMoney);
    	pubgFinanceFlowDetail.setTradeMoneyAfter(pubgUserTeam.getUserTeamBalance() + optMoney);
    	pubgFinanceFlowDetail.setTradeTypeId(TradeType.EGG_ADD.key());
    	pubgFinanceFlowDetail.setTradeTypeName(TradeType.EGG_ADD.value());
    	if (optType == 2) {
    		pubgFinanceFlowDetail.setTradeTypeId(TradeType.EGG_REDUCE.key());
    		pubgFinanceFlowDetail.setTradeTypeName(TradeType.EGG_REDUCE.value());
		}
    	this.pubgFinanceFlowDetailService.saveObj(pubgFinanceFlowDetail);
    	log.info("更新战队余额成功！");
    }
	
	/**
	 * 移除队员并更新当前战队的队员总数
	 * 
	 * @author 朱建文 创建时间：2018年4月3日 下午7:56:37
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) //任何异常 都需要回滚
	public boolean removeUser(Long clubId, Long teamId, Long userId) throws Exception {
		boolean isSuccess = false;
		try {
		
			// 更新队员-战队关系
			StringBuffer sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_user_team ");
			sb.append(" SET ");
			sb.append("   RELATION_STATE = ? ");
			sb.append(" WHERE ");
			sb.append("   USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ?");
			this.baseDao.safeUpdate(sb.toString(), 1, PubgConstant.USER_TEAM_RELATION_STATE_4, userId, teamId, clubId);
			
			// 更新队员-俱乐部关系
			sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_user_club ");
			sb.append(" SET ");
			sb.append("   RELATION_STATE = ? ");
			sb.append(" WHERE ");
			sb.append("   USER_ID = ? AND CLUB_ID = ?");
			this.baseDao.safeUpdate(sb.toString(), 1,PubgConstant.USER_TEAM_RELATION_STATE_4, userId, clubId);
			
			
			// 查询当前战队的总队员数
			sb = new StringBuffer();
			sb.append(" SELECT COUNT(0) ");
			sb.append(" FROM pubg_user_team ");
			sb.append(" WHERE USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND RELATION_STATE = ?");
			int i = this.baseDao.findCount(sb.toString(), userId, teamId, clubId, PubgConstant.USER_TEAM_RELATION_STATE_2);
			
			// 更新当前战队的总队员数
			sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_team ");
			sb.append(" SET ");
			sb.append("   MEMBER_NUM = ? ");
			sb.append(" WHERE ");
			sb.append("   ROW_ID = ? AND CLUB_ID = ?");
			this.baseDao.update(sb.toString(), i, teamId, clubId);
		} catch (Exception e) {
			throw new RuntimeException("移除队员并更新当前战队的队员总数出错！！", e);
		}
		return isSuccess;
	}
	
	/**
	 * 获取队员加入比赛审核列表
	 * 
	 * @author 朱建文 创建时间：2018年4月11日 下午4:08:42
	 */
	public Map<String, Object> getAuditMemberJoinMacthList(Long matchId, Long teamId, String userName) throws Exception {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		try {
			
			// 查询 报名人数、通过人数
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT COUNT(IF(USER_STATE = '10', 0, NULL)) applyNum, COUNT(IF(USER_STATE = '20', 0, NULL)) passNum ");
			sb.append(" FROM pubg_user_match ");
			sb.append(" WHERE MATCH_ID = ? AND TEAM_ID = ? AND USER_STATE IN ('10', '20')");
			dataMap = this.baseDao.findMap(sb.toString(), matchId, teamId);
			
			// 查询列表数据
			sb = new StringBuffer();
			sb.append(" SELECT ");
			sb.append("   b.ROW_ID rowId, IFNULL(b.PHOTO, '') photo, IFNULL(b.NICKNAME, '') nickname, b.USER_ACCOUNT userAccount, ");
			sb.append("   b.GAME_ROLE gameRole, IFNULL(b.BALANCE, 0) balance, a.USER_STATE userState ");
			sb.append(" FROM ");
			sb.append("   pubg_user_match a LEFT JOIN pubg_user b ON a.USER_ID = b.ROW_ID ");
			sb.append(" WHERE ");
			sb.append("   MATCH_ID = ? AND TEAM_ID = ? AND USER_STATE IN ('10', '20')");
			if (userName != null && userName.length() > 0) {
				sb.append("  AND (b.USER_ACCOUNT LIKE '%" + userName + "%' OR b.NICKNAME LIKE '%" + userName + "%') ");
			}
			List<Map<String, Object>> dataList = this.baseDao.findMapList(sb.toString(), matchId, teamId);
			dataMap.put("data", dataList);
		} catch (Exception e) {
			throw new RuntimeException("获取队员加入比赛审核列表出错！", e);
		}
		return dataMap;
	}
	
	/**
	 * 审核队员是否加入比赛
	 * 
	 * @author 朱建文 创建时间：2018年4月8日 下午2:20:22
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) //任何异常 都需要回滚
	public void auditMemberIsJoinMacth(PubgUserMatch pubgUserMatch, Integer auditState, Double userTeamBalance, Double userBalance) throws Exception{
		try {
			StringBuffer sb = new StringBuffer();
			StringBuffer updateMatch = new StringBuffer();
			updateMatch.append(" UPDATE ");
			updateMatch.append("   pubg_user_match ");
			updateMatch.append(" SET ");
			updateMatch.append("   USER_STATE = ? ");
//			if (2 == auditState) {
//				updateMatch.append(" , APPLY_PEOPLE_NUM = APPLY_PEOPLE_NUM - 1 ");
//			}
			updateMatch.append(" WHERE ");
			updateMatch.append("   USER_ID = ? AND TEAM_ID = ? AND MATCH_ID = ? AND USER_STATE = ? ");
			
			// 同意
			if (1 == auditState) {
				
				// 更新用户-赛事 状态
				this.baseDao.safeUpdate(updateMatch.toString(), 1, UserMatchConstant.USER_MATCH_20.getId(), 
						pubgUserMatch.getUserId(), pubgUserMatch.getTeamId(), 
						pubgUserMatch.getMatchId(), UserMatchConstant.USER_MATCH_10.getId());
				
				// 更新用户-战队的参赛数
				sb = new StringBuffer();
				sb.append(" UPDATE ");
				sb.append("   pubg_user_team ");
				sb.append(" SET ");
				sb.append("   MATCH_AMOUNT = MATCH_AMOUNT + 1 ");
				sb.append(" WHERE ");
				sb.append("   USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND RELATION_STATE = 2 ");
				this.baseDao.safeUpdate(sb.toString(), 1, 
						pubgUserMatch.getUserId(), pubgUserMatch.getTeamId(), pubgUserMatch.getClubId());
				
				// 更新用户-俱乐部的参赛数
				sb = new StringBuffer();
				sb.append(" UPDATE ");
				sb.append("   pubg_user_club ");
				sb.append(" SET ");
				sb.append("   MATCH_AMOUNT = MATCH_AMOUNT + 1 ");
				sb.append(" WHERE ");
				sb.append("   USER_ID = ? AND CLUB_ID = ? AND RELATION_STATE = 2 ");
				this.baseDao.safeUpdate(sb.toString(), 1, pubgUserMatch.getUserId(), pubgUserMatch.getClubId());
				
				// 更新用户的参赛数
				sb = new StringBuffer();
				sb.append(" UPDATE ");
				sb.append("   pubg_user ");
				sb.append(" SET ");
				sb.append("   MATCH_AMOUNT = MATCH_AMOUNT + 1 ");
				sb.append(" WHERE ");
				sb.append("   ROW_ID = ? AND USER_ROLE = ? ");
				this.baseDao.safeUpdate(sb.toString(), 1, pubgUserMatch.getUserId(), UserConstant.USER_ROLE_10.getId());
				
				// 更新比赛的参赛人数
				sb = new StringBuffer();
				sb.append(" UPDATE ");
				sb.append("   pubg_match ");
				sb.append(" SET ");
				sb.append("   MATCH_PEOPLE_NUM = MATCH_PEOPLE_NUM + 1, APPLY_SUM_TEMP = APPLY_SUM_TEMP + ? ");
				sb.append(" WHERE ");
				sb.append("   ROW_ID = ? AND CLUB_ID = ? AND MATCH_PEOPLE_NUM = ? ");
				this.baseDao.safeUpdate(sb.toString(), 1, pubgUserMatch.getApplyFee(),
						pubgUserMatch.getMatchId(), pubgUserMatch.getClubId(), 
						pubgUserMatch.getMatchPeopleNum());
				
				return;
			}
			
			// 拒绝
			// 扣款退还
			sb = new StringBuffer();
			sb.append(" UPDATE ");
			sb.append("   pubg_user_team ");
			sb.append(" SET ");
			sb.append("   USER_TEAM_BALANCE = USER_TEAM_BALANCE + ? ");
			sb.append(" WHERE ");
			sb.append("   USER_ID = ? AND TEAM_ID = ? AND CLUB_ID = ? AND USER_TEAM_BALANCE = ? ");
			this.baseDao.safeUpdate(sb.toString(), 1, pubgUserMatch.getApplyFee(), 
					pubgUserMatch.getUserId(), pubgUserMatch.getTeamId(), 
					pubgUserMatch.getClubId(), userTeamBalance);
			
			// 用户总余额 退还
			sb = new StringBuffer(255);
			sb.append(" UPDATE ");
			sb.append("   pubg_user ");
			sb.append(" SET ");
			sb.append("   BALANCE = BALANCE + ? ");
			sb.append(" WHERE ");
			sb.append("   ROW_ID = ? AND USER_ROLE = ? AND BALANCE = ?");
			this.baseDao.safeUpdate(sb.toString(), 1, pubgUserMatch.getApplyFee(), 
					pubgUserMatch.getUserId(), UserConstant.USER_ROLE_10.getId(), userBalance);
						
			// 财务流水
			PubgFinanceFlowDetail pubgFinanceFlowDetail = new PubgFinanceFlowDetail();
			pubgFinanceFlowDetail.setClubId(pubgUserMatch.getClubId());
			pubgFinanceFlowDetail.setClubName(pubgUserMatch.getClubName());
			pubgFinanceFlowDetail.setTeamId(pubgUserMatch.getTeamId());
			pubgFinanceFlowDetail.setTeamName(pubgUserMatch.getTeamName());
			pubgFinanceFlowDetail.setUserId(pubgUserMatch.getUserId());
			pubgFinanceFlowDetail.setUserAccount(pubgUserMatch.getUserAccount());
			pubgFinanceFlowDetail.setInOutFlag(1);
			pubgFinanceFlowDetail.setTradeMoney(pubgUserMatch.getApplyFee());
			pubgFinanceFlowDetail.setTradeMoneyAfter(userTeamBalance + pubgUserMatch.getApplyFee());
			pubgFinanceFlowDetail.setTradeTypeId(TradeType.MATCH_REFUSE_ADD.key());
			pubgFinanceFlowDetail.setTradeTypeName(TradeType.MATCH_REFUSE_ADD.value());
			pubgFinanceFlowDetail.setRelationOrderNo(pubgUserMatch.getMatchId() + "_" + pubgUserMatch.getMatchName());
			pubgFinanceFlowDetail.setCreateTime(DateUtil.getCurrentDateTime());
			this.pubgFinanceFlowDetailService.saveObj(pubgFinanceFlowDetail);
			
			// 更新用户-赛事 状态
			this.baseDao.safeUpdate(updateMatch.toString(), 1, UserMatchConstant.USER_MATCH_30.getId(), 
					pubgUserMatch.getUserId(), pubgUserMatch.getTeamId(), 
					pubgUserMatch.getMatchId(), UserMatchConstant.USER_MATCH_10.getId());
		} catch (Exception e) {
			throw new RuntimeException("审核队员是否加入比赛出错！", e);
		}
	}
	
}
